
package com.wyzz.hr.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.base.io.Exportinfo;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.ISalaryPaidBo;
import com.wyzz.hr.exportfile.ExportSalary;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.impl.PropertiesFileConfigManager;

public class MySalaryPaid extends BaseAction {

	public MySalaryPaid() {
		page = new Pager();
		startYear = DateUtil.formatTodayToS("yyyy");
		startMonth = "01";
		endYear = DateUtil.formatTodayToS("yyyy");
		endMonth = DateUtil.formatTodayToS("MM");
		docStream = null;
		contentDisposition = null;
		serverFileName = null;
	}

	public String execute() throws Exception {
		if (StringUtils.isEmpty(empId)) {
			empId = getCurrentEmpNo();
			empName = getCurrentEmp().getEmpName();
		}
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		Employee employee = empBo.loadEmp(empId, null);
		if (employee == null) {
			addErrorInfo("\u4E0D\u5B58\u5728\u6B64\u5458\u5DE5\uFF01");
			return "error";
		}
		DetachedCriteria dcPay = searchPay_DC();
		addCriteria(dcPay);
		getDrillDownList();
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		List espList = salaryPaidBo.findMySalaryPaid(dcPay, page);
		setSalaryPaidList(salaryPaidBo.searchMySalaryPaid(espList,
				(new StringBuilder()).append(startYear).append(startMonth)
						.toString(), (new StringBuilder()).append(endYear)
						.append(endMonth).toString()));
		setSalaryPaidList(espList);
		if ("export".equals(searchOrExport)) {
			if (espList == null || espList.size() == 0) {
				addActionError("\u65E0\u85AA\u8D44\u53D1\u653E\u6570\u636E\u53EF\u4EE5\u5BFC\u51FA\uFF01");
				return "success";
			}
			try {
				List exportResult = espList;
				Exportinfo exportSalary = new Exportinfo();
				IEmpSalaryAcctitemsBo esaitemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
						.getBean("empsalaryacctitemsBo");
				List titles[] = exportSalary.getSalarySheetAndTitle(
						exportResult, esaitemsBo);
				List results[] = exportSalary.getSalaryResult(exportResult,
						titles);
				String filePath = FileOperate.getFileHomePath();
				String xslFileName = (new StringBuilder())
						.append("export-")
						.append(FileOperate.buildFileName())
						.append(
								PropertiesFileConfigManager.getInstance()
										.getProperty("sys.saveDir.type").trim())
						.toString();
				filePath = (new StringBuilder()).append(filePath).append(
						xslFileName).toString();
				ExportSalary export = new ExportSalary(results, titles, true);
				export.export(filePath);
				File file = new File(filePath);
				FileInputStream fileInputStream = new FileInputStream(file);
				setDocStream(fileInputStream);
				String downFileName = (new StringBuilder()).append("(").append(
						startYear).append("-").append(startMonth).append("to")
						.append(endYear).append("-").append(endMonth).append(
								")").append("mySalaryPaid.xls").toString();
				setContentDisposition((new StringBuilder()).append(
						"filename=\"").append(downFileName).append("\"")
						.toString());
				setServerFileName(filePath);
				clearErrorsAndMessages();
				return "download";
			} catch (Exception e) {
				addActionError("\u6570\u636E\u5BFC\u51FA\u5931\u8D25\uFF01");
				e.printStackTrace();
				return "success";
			}
		} else {
			return "success";
		}
	}

	private DetachedCriteria searchPay_DC() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalarypay.class);
		dc.createAlias("espEmpno", "emp", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_PB_NO).toString(), "pb", 1);
		dc.setFetchMode("espEmpconfig", FetchMode.JOIN);
		dc.setFetchMode("espEmpconfig.escJobgrade", FetchMode.JOIN);
		dc.setFetchMode("espEmptype", FetchMode.JOIN);
		dc.setFetchMode("espJobgrade", FetchMode.JOIN);
		dc.setFetchMode("espEsavId", FetchMode.JOIN);
		dc.setFetchMode("espEsavId.esavEsac", FetchMode.JOIN);
		dc.createAlias((new StringBuilder())
				.append(Empsalarypay.PROP_ESP_EMPNO).append(".").append(
						Employee.PROP_EMP_DEPT_NO).toString(), "empOrgDept", 1);
		dc.createAlias((new StringBuilder())
				.append(Empsalarypay.PROP_ESP_EMPNO).append(".").append(
						Employee.PROP_EMP_LOCATION_NO).toString(), "empOrgLoc",
				1);
		dc.createAlias((new StringBuilder())
				.append(Empsalarypay.PROP_ESP_EMPNO).append(".").append(
						Employee.PROP_EMP_TYPE).toString(), "empType", 1);
		return dc;
	}

	private void addCriteria(DetachedCriteria dc) {
		dc.add(Restrictions.eq("emp.id", empId));
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_ID).toString(), "eq", new String[] { empId });
		BaseCrit.addDC(dc, Empsalarypay.PROP_ESP_YEARMONTH, "between",
				new String[] {
						(new StringBuilder()).append(startYear).append(
								startMonth).toString(),
						(new StringBuilder()).append(endYear).append(endMonth)
								.toString() });
	}

	private void getDrillDownList() {
		years = getDrillDown("EmpSalaryPeriod", new String[0]);
		System.out.println(years.size()+"asfdasf");
	}

	public String[] defaultComputePay(String empId) {
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		String result[] = new String[4];
		String maxYearmonth = DateUtil.formatDateToS(new Date(), "yyyyMM");
		String dateArr[] = new String[12];
		Calendar cal = Calendar.getInstance();
		Date temp = null;
		temp = DateUtil.parseDateByFormat(maxYearmonth, "yyyyMM");
		cal.setTime(temp);
		for (int i = 0; i < 12; i++) {
			dateArr[i] = DateUtil.formatDateToS(cal.getTime(), "yyyyMM");
			cal.add(2, -1);
		}

		result[0] = salaryPaidBo.getGivenMonthAvgPay(empId, dateArr);
		result[2] = (new StringBuilder()).append(
				DateUtil.formatDateToS(DateUtil.parseDateByFormat(dateArr[11],
						"yyyyMM"), "yyyy\u5E74MM\u6708")).append("-").append(
				DateUtil.formatDateToS(DateUtil.parseDateByFormat(dateArr[0],
						"yyyyMM"), "yyyy\u5E74MM\u6708")).toString();
		temp = new Date();
		cal.setTime(temp);
		cal.add(1, -1);
		cal.set(2, 0);
		for (int i = 0; i < 12; i++) {
			dateArr[i] = DateUtil.formatDateToS(cal.getTime(), "yyyyMM");
			cal.add(2, 1);
		}

		result[1] = salaryPaidBo.getGivenMonthAvgPay(empId, dateArr);
		result[3] = (new StringBuilder()).append(
				DateUtil.formatDateToS(DateUtil.parseDateByFormat(dateArr[0],
						"yyyyMM"), "yyyy\u5E74MM\u6708")).append("-").append(
				DateUtil.formatDateToS(DateUtil.parseDateByFormat(dateArr[11],
						"yyyyMM"), "yyyy\u5E74MM\u6708")).toString();
		return result;
	}

	public String givenComputePay(String empId, String start, String end) {
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		Date startDate = DateUtil.parseDateByFormat(start, "yyyyMM");
		Date endDate = DateUtil.parseDateByFormat(end, "yyyyMM");
		List dateList = new ArrayList();
		Calendar cal = Calendar.getInstance();
		for (Date tempDate = startDate; tempDate.compareTo(endDate) <= 0; tempDate = cal
				.getTime()) {
			dateList.add(DateUtil.formatDateToS(tempDate, "yyyyMM"));
			cal.setTime(tempDate);
			cal.add(2, 1);
		}

		String dateArr[] = new String[dateList.size()];
		for (int i = 0; i < dateList.size(); i++)
			dateArr[i] = (String) dateList.get(i);

		String result = salaryPaidBo.getGivenMonthAvgPay(empId, dateArr);
		return result;
	}

	public String getEndMonth() {
		return endMonth;
	}

	public void setEndMonth(String endMonth) {
		this.endMonth = endMonth;
	}

	public String getStartMonth() {
		return startMonth;
	}

	public void setStartMonth(String startMonth) {
		this.startMonth = startMonth;
	}

	public String getEndYear() {
		return endYear;
	}

	public void setEndYear(String endYear) {
		this.endYear = endYear;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public List getYears() {
		return years;
	}

	public void setYears(List years) {
		this.years = years;
	}

	public List getSalaryPaidList() {
		return salaryPaidList;
	}

	public void setSalaryPaidList(List salaryPaidList) {
		this.salaryPaidList = salaryPaidList;
	}

	public String getStartYear() {
		return startYear;
	}

	public void setStartYear(String startYear) {
		this.startYear = startYear;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public FileInputStream getDocStream() {
		return docStream;
	}

	public void setDocStream(FileInputStream docStream) {
		this.docStream = docStream;
	}

	public String getServerFileName() {
		return serverFileName;
	}

	public void setServerFileName(String serverFileName) {
		this.serverFileName = serverFileName;
	}

	private static final long serialVersionUID = 0xfffffffebd50f49fL;
	private Pager page;
	private List salaryPaidList;
	private List years;
	private String startYear;
	private String startMonth;
	private String endYear;
	private String endMonth;
	private String empId;
	private String empName;
	private String searchOrExport;
	private FileInputStream docStream;
	private String contentDisposition;
	private String serverFileName;
}
